Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | /** * Number formatting utilities with i18n support * * Provides consistent number formatting across the application * based on the user's locale. */ import i18n from '@/lib/i18n'; /** * Get the current locale from i18n */ export function getCurrentLocale(): string { return i18n.language || 'en'; } /** * Format a number according to the current locale * * @param value - Number to format * @param options - Intl.NumberFormat options * @returns Formatted number string */ export function formatNumber( value: number, options?: Intl.NumberFormatOptions ): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); return new Intl.NumberFormat(locale, options).format(value); } /** * Format a number as currency according to the current locale * * @param value - Number to format * @param currency - Currency code (e.g., 'USD', 'EUR', 'GBP') * @param options - Additional Intl.NumberFormat options * @returns Formatted currency string */ export function formatCurrency( value: number, currency: string = 'USD', options?: Intl.NumberFormatOptions ): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); const defaultOptions: Intl.NumberFormatOptions = { style: 'currency', currency, ...options}; return new Intl.NumberFormat(locale, defaultOptions).format(value); } /** * Format a number as a percentage according to the current locale * * @param value - Number to format (0.5 = 50%) * @param options - Additional Intl.NumberFormat options * @returns Formatted percentage string */ export function formatPercent( value: number, options?: Intl.NumberFormatOptions ): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); const defaultOptions: Intl.NumberFormatOptions = { style: 'percent', minimumFractionDigits: 0, maximumFractionDigits: 2, ...options}; return new Intl.NumberFormat(locale, defaultOptions).format(value); } /** * Format a number with a specific number of decimal places * * @param value - Number to format * @param decimals - Number of decimal places (default: 2) * @returns Formatted number string */ export function formatDecimal(value: number, decimals: number = 2): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); return new Intl.NumberFormat(locale, { minimumFractionDigits: decimals, maximumFractionDigits: decimals}).format(value); } /** * Format a number with thousand separators * * @param value - Number to format * @returns Formatted number string with thousand separators */ export function formatWithSeparators(value: number): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); return new Intl.NumberFormat(locale, { useGrouping: true}).format(value); } /** * Format a number in compact notation (e.g., 1.2K, 3.4M) * * @param value - Number to format * @param options - Additional Intl.NumberFormat options * @returns Formatted compact number string */ export function formatCompact( value: number, options?: Intl.NumberFormatOptions ): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); const defaultOptions: Intl.NumberFormatOptions = { notation: 'compact', compactDisplay: 'short', ...options}; return new Intl.NumberFormat(locale, defaultOptions).format(value); } /** * Format a number in scientific notation * * @param value - Number to format * @param options - Additional Intl.NumberFormat options * @returns Formatted scientific notation string */ export function formatScientific( value: number, options?: Intl.NumberFormatOptions ): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); const defaultOptions: Intl.NumberFormatOptions = { notation: 'scientific', ...options}; return new Intl.NumberFormat(locale, defaultOptions).format(value); } /** * Format bytes to human-readable size (e.g., 1.5 MB, 500 KB) * * @param bytes - Size in bytes * @param decimals - Number of decimal places (default: 2) * @returns Formatted size string */ export function formatBytes(bytes: number, decimals: number = 2): string { if (bytes === 0) return '0 B'; if (bytes < 0 || isNaN(bytes)) return 'Invalid Size'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${formatDecimal(bytes / Math.pow(k, i), decimals)} ${sizes[i]}`; } /** * Format a number as an ordinal (e.g., 1st, 2nd, 3rd) * * @param value - Number to format * @returns Formatted ordinal string */ export function formatOrdinal(value: number): string { if (isNaN(value)) { return 'NaN'; } const locale = getCurrentLocale(); // Use Intl.PluralRules for proper ordinal formatting const pr = new Intl.PluralRules(locale, { type: 'ordinal' }); const rule = pr.select(value); const suffixes: Record<string, Record<string, string>> = { en: { one: 'st', two: 'nd', few: 'rd', other: 'th'}, es: { one: 'º', two: 'º', few: 'º', other: 'º'}, zh: { one: '第', two: '第', few: '第', other: '第'}}; const localeSuffixes = suffixes[locale] || suffixes.en; const suffix = localeSuffixes[rule] || localeSuffixes.other; // For Chinese, the ordinal comes before the number if (locale === 'zh') { return `${suffix}${value}`; } return `${value}${suffix}`; } /** * Format a rating (e.g., 4.5/5, 8.7/10) * * @param value - Rating value * @param max - Maximum rating (default: 10) * @param decimals - Number of decimal places (default: 1) * @returns Formatted rating string */ export function formatRating( value: number, max: number = 10, decimals: number = 1 ): string { if (isNaN(value)) { return 'N/A'; } return `${formatDecimal(value, decimals)}/${max}`; } /** * Format a duration in seconds to human-readable format * * @param seconds - Duration in seconds * @param short - Use short format (e.g., "1h 23m" vs "1 hour 23 minutes") * @returns Formatted duration string */ export function formatDuration(seconds: number, short: boolean = true): string { if (seconds < 0 || isNaN(seconds)) { return '0m'; } const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const secs = Math.floor(seconds % 60); if (short) { if (hours > 0 && minutes > 0) { return `${hours}h ${minutes}m`; } else if (hours > 0) { return `${hours}h`; } else if (minutes > 0) { return `${minutes}m`; } else { return `${secs}s`; } } else { const parts: string[] = []; if (hours > 0) parts.push(`${hours} hour${hours !== 1 ? 's' : ''}`); if (minutes > 0) parts.push(`${minutes} minute${minutes !== 1 ? 's' : ''}`); if (secs > 0 && hours === 0) parts.push(`${secs} second${secs !== 1 ? 's' : ''}`); return parts.join(' ') || '0 seconds'; } } /** * Format a number range (e.g., "10-20", "100-200") * * @param start - Start of range * @param end - End of range * @param options - Intl.NumberFormat options * @returns Formatted range string */ export function formatRange( start: number, end: number, options?: Intl.NumberFormatOptions ): string { if (isNaN(start) || isNaN(end)) { return 'Invalid Range'; } const locale = getCurrentLocale(); const formatter = new Intl.NumberFormat(locale, options); // Capture format method before 'in' check to avoid TypeScript narrowing issue const format = formatter.format.bind(formatter); // Use formatRange if available (newer browsers) if ('formatRange' in formatter) { return (formatter as any).formatRange(start, end); } // Fallback for older browsers return `${format(start)}-${format(end)}`; } /** * Parse a localized number string to a number * * @param value - Localized number string * @returns Parsed number */ export function parseLocalizedNumber(value: string): number { const locale = getCurrentLocale(); // Get the decimal and thousand separators for the current locale const parts = new Intl.NumberFormat(locale).formatToParts(1234.5); const decimalSeparator = parts.find(p => p.type === 'decimal')?.value || '.'; const thousandSeparator = parts.find(p => p.type === 'group')?.value || ','; // Remove thousand separators and replace decimal separator with '.' const normalized = value .replace(new RegExp(`\\${thousandSeparator}`, 'g'), '') .replace(decimalSeparator, '.'); return parseFloat(normalized); } |